var product ;   //商品对象
var source;  //来源
var serviceUrl = $api.getStorage('serviceUrl'); //服务器地址	

apiready = function() {
		
		
		api.showProgress({
	        title: '加载中...',
	        modal: false
	    });
		product = api.pageParam.product;
		product.num = 1;
			
		console.log(JSON.stringify(product));
		
	//	openScrollPicture();
	    
	    source=api.pageParam.source;
	    if(source=='抢购'){
	        $api.css($api.byId('buy_btn'),'display:none');
	        
	    }
	    
	    if(source=='scan'){  //扫码
	        $api.css($api.byId('cart_but'),'display:none');
	        $api.attr($api.byId('buy_but'), 'class', 'am-u-sm-12');
	        
	    }
	    
	    if(product.type == '服务产品'){
	    	var html = '<button type="button" onclick="api.call({type: \'tel_prompt\',number:\'4009923568\'});" style="border-radius:5px;margin-left: -16px; width: 100px;" class="am-btn am-btn-primary am-btn-block">';
			html += '咨询电话';
			html += '</button>';

	    	$api.html($api.byId('cart_but'), html);
		}
		
        $api.attr($api.byId('proDefImg'),'src',serviceUrl+"/files/"+(product.proDefImg||0));
        
	    $api.html($api.byId('pName'), product.name);
		$api.html($api.byId('pprice'), '￥'+product.price.toFixed(2));
		$api.html($api.byId('isfreefreight'), '包邮：' + product.isfreefreight);
		$api.html($api.byId('pspec'), '规格：'+product.spec);
		$api.html($api.byId('brand'), '品牌：'+product.brand);
		$api.html($api.byId('place'), '产地：'+product.place);
		
		
		if(!product.isfreefreight || product.isfreefreight == ''){
			$api.css($api.byId('isfreefreight'), 'display:none');
		}
		
		if(!product.spec||product.spec==''){
		    $api.css($api.byId('pspec'),'display:none');
			//$api.html($api.byId('pspec'), '规格：'+product.spec);
		}
		
		if(!product.brand||product.brand==''){
		    $api.css($api.byId('brand'),'display:none');
			//$api.html($api.byId('brand'), '品牌：'+product.brand);
		}
		
		if(!product.place||product.place==''){
		    $api.css($api.byId('place'),'display:none');
			//$api.html($api.byId('place'), '产地：'+product.place);
		}
	
		api.hideProgress();
			
		
		showDetail();
}



//设置滑动图片
function openScrollPicture(){

    var paths = ['http://wen-du.com/webapp/images/ih1.jpg', 'http://wen-du.com/webapp/images/ih2.jpg'];

   
  //  for(var i=0;i<advList.length;i++){
	 //    paths.push(serviceUrl+product.proDefImg);
//	}

	var obj = api.require('UIScrollPicture');
	
	
	obj.open({
	    rect: {
	        x: 0, y: 0, w:api.winWidth, h:195
	    },
	    data: {
	        paths: paths,
	    },
	    styles: {
	        indicator: {
	            align: 'center',
	            color: '#666',
	            activeColor: '#ffaa43'
	        }
	    },
	    placeholderImg: 'widget://res/slide1.jpg',
	    contentMode: 'scaleToFill',
	    interval: 3,
	    loop: true,
	    fixedOn: 'productinfo',
	    fixed: false
	}, function(ret, err){
	     if(ret.status){
	        if(ret.eventType == 'click'){
	            //点击图片的操作
	            //alert(ret.index);
	        }
	     }
	});

    
    
};		
		
		
//加法
function add(){
    ++product.num;
	$api.val($api.byId('buyNum'),product.num);
	
	$api.addCls($api.byId('reduce'), 'active');
	
};

//减
function reduce(){
   if(product.num > 1){
		--product.num;
		$api.val($api.byId('buyNum'),product.num);
	}
	
	if(product.num ==1){
	    $api.removeCls($api.byId('reduce'), 'active'); 
	}

};


//查看简介
function openDesc(){

    	var data={
    	    'prodesc':product.info,
    	    'comments':product.comment,
    	}
    	openWin('prodetail',{data:data});
    	
}


//加入购物车
function addCrat(){
   addCratItem(product._id);
};

//立即购买
function buy(){

    var account = $api.getStorage('account');
	if (!account) {
	   
	   if(source=='scan'){
	      openWin('buy',{product: product,source:source});
	   }else{
	      openWin('login');
	   }
		
	}else{
		openWin('buy',{product: product,source:source});
    }

};


//分享到微信
function swipe_title_share(){

    var wx = api.require('wx');
	wx.isInstalled(function(ret, err){
		if(ret.installed){
			// alert("当前设备已安装微信客户端");
			wx.shareWebpage({
				scene: 'session',
				title: '72公社',
				description:product.name,
				thumb: 'icon/icon150x150.png',
				contentUrl: 'http://comm.72gs.net/shop/proshow?id='+product._id
			}, function(ret, err){
				if(ret.status){
			        alert('分享成功');
			    }else{
			        if(err.code==2){
			        	alert('取消分享');
			        }else{
			        	alert('分享失败');
			        }
			    }
			});
		}else{
				alert('当前设备未安装微信客户端');
		}
	});
   

};

		
		
function showDetail(){
	api.parseTapmode();//解析元素tapmode属性,优化点击事件处理
        
    var data={
	    'prodesc':product.info,
	    'comments':product.comment,
	}
    
    var $content=$api.byId('tabmain');   //内容区
    var $tabcontent = $api.domAll($content, '.tabcontent');
    
    //选项卡
    var tab = $api.domAll("#tab li");
    for(var i in tab){
        $api.addEvt(tab[i], 'click', function(){
            $api.removeCls($api.dom("#tab li.active"),'active');
            $api.addCls(this,'active');              //选项卡切换
            
           
            for (var i = 0; i < $tabcontent.length; i++) {
		        $api.removeCls($tabcontent[i], 'activeTab');
		    }
		    var tabid=$api.attr(this, 'name');
            $api.addCls($api.byId(tabid), 'activeTab');      //内容切换
            
        });
        
    }
     
    
    $api.html($api.byId('prodetail'), data.prodesc);
    
    var html="";
    if(data.comments.length>0){
      for(var i=0;i<data.comments.length;i++){
           html+='<div style="font-size:14px;">';
	           html+='<div class="am-u-sm-12" style="padding:5px 0px;">';
		           html+='<div class="am-u-sm-6">';
		           for(var j=0;j<data.comments[i].satisfaction;j++){
		               html+='<i class="am-icon-star" style="color: #fc7d0d"></i>';
		           }
		           html+='</div>';
		           html+='<div class="am-u-sm-6" style="padding:0px; font-size:14px;color:gray">';
		           html+=data.comments[i].nickName+'&nbsp;&nbsp;&nbsp;'+data.comments[i].commentTime;
		           html+='</div>';
	           html+='</div>';
	           html+='<div class="am-u-sm-12 am-text-truncate" style="border-bottom:1px solid #eee;">';
	               html+=data.comments[i].content;
	           html+='</div>';
	        html+='</div>';
           
      }
    
    }else{
       html+=' <div class="am-u-sm-12 am-text-center"><br/>暂无评论...</div>';
    }
    
    $api.html($api.byId('procomment'), html);
}
		
	
	    